package com.wise.config;

import com.microsoft.azure.storage.StorageException;
import com.microsoft.azure.storage.blob.CloudBlobContainer;
import com.microsoft.azure.storage.blob.CloudBlockBlob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author larry
 * @date 2019-11-19
 */
@Component
public class CloudBlockBlobCreator {
    private static final ReentrantLock lock = new ReentrantLock();

//    private static final Map<String, CloudBlockBlob> map = new HashMap<>();

    @Autowired
    CloudBlobContainer cloudBlobContainer;

    public CloudBlockBlob getBlockBlob(String id, String downloadFileName) throws URISyntaxException, StorageException {
        lock.lock();
        CloudBlockBlob cloudBlockBlob;
        try {
//            cloudBlockBlob = map.get(id);
//            if (null == cloudBlockBlob) {
                cloudBlockBlob = cloudBlobContainer.getBlockBlobReference(id);
                cloudBlockBlob.getProperties().setContentType("application/octet-stream");
                cloudBlockBlob.getProperties().setContentDisposition("attachment; filename="+downloadFileName);
//                map.put(id, cloudBlockBlob);
//            }
            return cloudBlockBlob;
        }finally {
            lock.unlock();
        }
    }

}
